﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class Example1 : MonoBehaviour
{

    public AssetReference prefabRef;
    private void OnGUI()
    {
        if (GUILayout.Button("第一种"))
        {
            StartInstantiate1();
        }
        if (GUILayout.Button("第二种"))
        {
            StartCoroutine(StartInstantiate2());
        }
        if (GUILayout.Button("第三种"))
        {
            StartInstantiate3();
        }
        if (GUILayout.Button("第四种"))
        {
            StartInstantiate4();
        }
    }

    //第一种
    void StartInstantiate1()
    {
        prefabRef.InstantiateAsync();
    }
    //第二种，使用协程
    IEnumerator StartInstantiate2()
    {
        var handle = prefabRef.InstantiateAsync();
        yield return handle;
        var go = handle.Result;
    }
    //第三种
    void StartInstantiate3()
    {
        var handle = prefabRef.InstantiateAsync();
        handle.Completed += go =>
        {
            var cube = go.Result;
            Debug.Log(cube.name);
        };
    }

    //第四种
    void StartInstantiate4()
    {
        var handle = prefabRef.InstantiateAsync();
        handle.Completed += go =>
        {
            var cube = go.Result;
            Debug.Log(cube.name);
        };
    }
    private void Loaded(AsyncOperationHandle<GameObject> obj)
    {
        var cube = obj.Result;
        Debug.Log(cube.name);
    }


}
